package com.sjx.boundarycallback.decoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by sjx on 5/31/22
 * 自定义ItemDecoration
 */
public class LinearItemDecoration extends RecyclerView.ItemDecoration {

    private final Paint mNormalPaint = new Paint();
    private final Paint mRedPaint = new Paint();

    public LinearItemDecoration() {
        super();
        mNormalPaint.setColor(Color.GREEN);
        mRedPaint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int left;
        super.onDraw(c, parent, state);
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        int childCount = parent.getChildCount();
        View childFirst = parent.getChildAt(0);
        View childLast = parent.getChildAt(childCount - 1);
        if (manager == null) return;
        left = manager.getLeftDecorationWidth(childFirst);
        c.drawLine((float) left / 2, (float) childFirst.getHeight() / 2, 50, childLast.getY() + (float) childFirst.getHeight() / 2, mRedPaint);
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int cx = left / 2;
            int cy = child.getTop() + child.getHeight() / 2;
            c.drawCircle(cx, cy, 20, mNormalPaint);
        }

    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = 1;
        outRect.left = 100;
    }
}
